package com.bc.bcapi.service;

import com.bc.bcmodel.NationalityInfo;
import com.bc.commoncore.util.CookieUtils;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.math.BigInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.bc.bcapi.dao.T_CountryMapper;
import com.bc.bcmodel.domain.T_CountryModel;

/**
 * @Name T_CountryService * @Description  * @Author generation * @Date 2018/9/19 16:32:26 * @Version 1.0
 **/
@Service
public class T_CountryService {
    @Resource
    T_CountryMapper t_CountryMapper;

    /**
     * 保存单条数据     *      * @return
     */
    public int insert(T_CountryModel req) {
        return t_CountryMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    public int insertBatch(List<T_CountryModel> req) {
        return t_CountryMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    public T_CountryModel findByNamedParam(T_CountryModel req) {
        return t_CountryMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    public List<T_CountryModel> getAllList(T_CountryModel req) {
        return t_CountryMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_CountryModel> getList(T_CountryModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_CountryModel> list = t_CountryMapper.findByNamedParamList(req);
        return new PageInfo<T_CountryModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    public int update(T_CountryModel req) {
        return t_CountryMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    public int delete(BigInteger id) {
        return t_CountryMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    public long count(T_CountryModel req) {
        return t_CountryMapper.count(req);
    }

    private String getCurrentLanguage(HttpServletRequest request, HttpServletResponse response,
                                      String platform_cookie_name) {
        String cookie = CookieUtils.getCookie(request, response, platform_cookie_name);
        String lan = "zh";
        if (!StringUtils.isEmpty(cookie)) {
            lan = cookie;
        }
        return lan;
    }

    private static List<NationalityInfo> _listCountryEn;
    private static List<NationalityInfo> _listCountryZh;
    private static final Lock lock = new ReentrantLock();

    public JsonResult getAllCountry(
            HttpServletRequest request, HttpServletResponse response,
            String platform_cookie_name) {
        String language = getCurrentLanguage(request, response, platform_cookie_name);
        if (language.equals("zh")) {
            //英文
            if (_listCountryZh == null) {
                lock.lock();
                try {
                    _listCountryZh = t_CountryMapper.getAllZhCountry();
                } catch (Exception e) {
                } finally {
                    lock.unlock();
                }
            }
            return JsonResult.get(_listCountryZh);
        }
        if (_listCountryEn == null) {
            lock.lock();
            try {
                _listCountryEn = t_CountryMapper.getAllEnCountry();
            } catch (Exception e) {
            } finally {
                lock.unlock();
            }
        }
        return JsonResult.get(_listCountryEn);

//        return null;
    }

}